<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

/**
 * Add a facebook's "I like" button
 *
 * @param url : url
 * @param layout : 'standard', 'button_count'
 * @param showfaces : 'true', 'false'
 * @param width : integer
 * @param action : 'like', 'recommend'
 * @param font : 'arial', 'lucida grande', 'segoe ui', 'tahoma', 'trebuchet ms', 'verdana'
 * @param colorscheme : 'light' or 'dark'
 * 
 * @return string
 *
 * @example {fbLike} => will retrieve current page url
 * @example {fbLike url="www.codizer.org"} => I like www.codizer.org ;-)
 */
function smarty_function_fbLike($params, &$smarty)
{    
    $url = isset($params['url']) ? $params['url'] : null;

    if(is_null($url))
    {
        $page = f_core_Context::getInstance()->getContext('currentPage');
        if(is_object($page) && $page->getModel() == 'modules_website/page')
        {
            $url = $page->getCompleteUrl();
        }
        else
        {
            $smarty->trigger_error('fbLike : no url found !');
        }
    }

    if(substr($url, 0, 7) != 'http://')
    {
        $url = 'http://'.$url;
    }

    if(!filter_var($url, FILTER_VALIDATE_URL))
    {
        $smarty->trigger_error('fbLike : url is not valid !');
    }

    $layouts = array('standard', 'button_count');
    $layout = isset($params['layout']) && in_array($params['layout'], $layouts) ? $params['layout'] : 'standard';

    $faces = array('true', 'false');
    $showFaces = isset($params['showfaces']) && in_array($params['showfaces'], $faces) ? $params['showfaces'] : 'false';

    $width = isset($params['width']) && is_integer($params['width']) ? $params['width'] : 450;

    $actions = array('like', 'recommend');
    $action = isset($params['action']) && in_array($params['action'], $actions) ? $params['action'] : 'like';

    $fonts = array('arial', 'lucida grande', 'segoe ui', 'tahoma', 'trebuchet ms', 'verdana');
    $font = isset($params['font']) && in_array($params['font'], $fonts) ? htmlentities($params['font']) : null;

    $schemes = array('light', 'dark');
    $colorscheme = isset($params['colorscheme']) && in_array($params['colorscheme'], $schemes) ? $params['colorscheme'] : 'light';

    $result = '<iframe src="http://www.facebook.com/plugins/like.php?';

    $res[] = 'href='.$url;
    $res[] = 'layout='.$layout;
    $res[] = 'show_faces='.$showFaces;
    $res[] = 'width='.$width;
    $res[] = 'action='.$action;
    $res[] = is_null($font) ? 'font' : 'font='.$href;
    $res[] = 'colorscheme='.$colorscheme;
    $res[] = 'height=80';

    $result .= join('&amp;', $res);

	$result .= '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>';

    echo $result;
}